package com.cskaoyan.mall.controller.systemmanage;

import com.cskaoyan.mall.bean.Admin;
import com.cskaoyan.mall.bean.Log;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Zsp
 * 2021/1/10
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("list")
    public ResultVO getAdminList(Integer page, Integer limit, String sort, String order, String username) {
        return ResultVO.ok(adminService.getAdminList(page, limit, sort, order, username));
    }

    @PostMapping("create")
    public ResultVO create(@RequestBody Admin admin, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "添加管理员", null, null, null, new Date(), new Date(), false);
        return adminService.storageAdminAndInfo(admin, log);

    }

    @PostMapping("update")
    public ResultVO update(@RequestBody Admin admin, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "修改管理员", null, null, null, new Date(), new Date(), false);
        return adminService.update(admin, log);
    }

    @PostMapping("delete")
    public ResultVO delete(@RequestBody Admin admin, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "删除管理员", null, null, null, new Date(), new Date(), false);
        return adminService.delete(admin, log);
    }
}
